/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jinyao.exploit.modules.gen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinyao.exploit.common.base.service.impl.CrudServiceImpl;
import com.jinyao.exploit.modules.gen.dao.SchemeDao;
import com.jinyao.exploit.modules.gen.dao.TableColumnDao;
import com.jinyao.exploit.modules.gen.dao.TableDao;
import com.jinyao.exploit.modules.gen.entity.Config;
import com.jinyao.exploit.modules.gen.entity.Scheme;
import com.jinyao.exploit.modules.gen.entity.Table;
import com.jinyao.exploit.modules.gen.entity.Template;
import com.jinyao.exploit.modules.gen.service.SchemeService;
import com.jinyao.exploit.modules.gen.util.GenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 生成方案Service
 * @author ThinkGem
 * @version 2013-10-15
 */
@Service
@Transactional(readOnly = true)
public class SchemeServiceImpl extends CrudServiceImpl<SchemeDao, Scheme> implements SchemeService {
//public class SchemeServiceImpl {

	@Autowired
	private TableDao tableDao;
	@Autowired
	private TableColumnDao tableColumnDao;

	@Transactional(readOnly = false)
	public boolean save(Scheme scheme) {
		boolean result = super.save(scheme);
		// 生成代码
		if ("1".equals(scheme.getFlag())){
			scheme.setGenerateCode(generateCode(scheme));
		}
		return result;
	}

	private String generateCode(Scheme scheme){

		StringBuilder result = new StringBuilder();

		// 查询主表及字段列
		Table table = tableDao.selectById(scheme.getTable().getId());
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("table_id", table.getId());
		table.setColumnList(tableColumnDao.selectList(queryWrapper));

		// 获取所有代码模板
		Config config = GenUtils.getConfig();

		// 获取模板列表
		List<Template> templateList = GenUtils.getTemplateList(config, scheme.getCategory(), false);
		List<Template> childTableTemplateList = GenUtils.getTemplateList(config, scheme.getCategory(), true);

		// 如果有子表模板，则需要获取子表列表
		if (childTableTemplateList.size() > 0){
			Table parentTable = new Table();
//			parentTable.setParent(table.getName());
			QueryWrapper queryWrapper2 = new QueryWrapper<>();
			queryWrapper2.eq("parent_id", parentTable.getId());
			table.setChildList(tableDao.selectList(queryWrapper2));
		}

		// 生成子表模板代码
		for (Table childTable : table.getChildList()){
//			childTable.setParent(table);
			QueryWrapper queryWrapper2 = new QueryWrapper<>();
			queryWrapper2.eq("table_id", childTable.getId());
			childTable.setColumnList(tableColumnDao.selectList(queryWrapper2));
			scheme.setTable(childTable);
			Map<String, Object> childTableModel = GenUtils.getDataModel(scheme);
			for (Template tpl : childTableTemplateList){
				result.append(GenUtils.generateToFile(tpl, childTableModel, scheme.getReplaceFile()));
			}
		}

		// 生成主表模板代码
		scheme.setTable(table);
		Map<String, Object> model = GenUtils.getDataModel(scheme);
		for (Template tpl : templateList){
			result.append(GenUtils.generateToFile(tpl, model, scheme.getReplaceFile()));
		}
		return result.toString();
	}
}
